package com.shaw.workrecord.controller;

import com.shaw.workrecord.entity.PageRequest;
import com.shaw.workrecord.entity.PageResponse;
import com.shaw.workrecord.entity.ResponseBean;
import com.shaw.workrecord.pojo.request.RecordGroupAddRequest;
import com.shaw.workrecord.pojo.request.RecordGroupEditRequest;
import com.shaw.workrecord.pojo.response.RecordGroupResponse;
import com.shaw.workrecord.service.IRecordGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags="账本记录组模块")
@Validated
public class RecordGroupController {

    @Autowired
    private IRecordGroupService service;

    @GetMapping("/recordGroup")
    @ApiOperation("账本分页列表")
    public ResponseBean<PageResponse<RecordGroupResponse>> getPage(PageRequest request) {
        return ResponseBean.success(service.getRecordGroupPage(request));
    }

    @PostMapping("/recordGroup")
    @ApiOperation("新增账本")
    public ResponseBean<Boolean> addRecordGroup(@RequestBody RecordGroupAddRequest request) {
        if (service.addRecordGroup(request)) {
            return ResponseBean.success(true);
        }
        return ResponseBean.failure("新增失败");
    }

    @PutMapping("/recordGroup")
    @ApiOperation("编辑账本")
    public ResponseBean<Boolean> editRecordGroup(@RequestBody RecordGroupEditRequest request) {
        if (service.editRecordGroup(request)) {
            return ResponseBean.success(true);
        }
        return ResponseBean.failure("编辑失败");
    }

    @DeleteMapping("/recordGroup/{id}")
    @ApiOperation("删除账本")
    public ResponseBean<Boolean> deleteRecordGroup(@PathVariable("id") Integer id) {
        if (service.deleteRecordGroup(id)) {
            return ResponseBean.success(true);
        }
        return ResponseBean.failure("删除失败");
    }
}
